package wmhsudoku.gui;

import wmhsudoku.board.AnealingSolver;
import wmhsudoku.board.Board;
import wmhsudoku.board.BruteForceSolver;
import wmhsudoku.board.Solver;


public class Main {


	private static Main main;
	MainFrame mainFrame;
	Board board = null;
	
	public static boolean RUNNING = true;
	public static int SIZE = 9;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		main = new Main();
		
	}
	
	public Main() {

		mainFrame = new MainFrame(this);
		mainFrame.setVisible(true);
		
		
	}
	
	public void newBoard(boolean tempChangeFunc, double initTemp, double tempChange,
			boolean printConsole, boolean printFile, int neighNumber, boolean neighFunc) {
		RUNNING = true;
		
		board = new Board();
		board.generateMask(1);
		mainFrame.setNewMap(board.getSudoku(), board.getMask());
		Solver solver = new AnealingSolver(board, "AnealingCreateLog.txt", tempChangeFunc, initTemp, tempChange, printConsole, printFile,
											neighNumber, neighFunc);
		solver.init();	
		paintThread(solver, true);
		
		
	}
	
	private void paintThread(final Solver solver, final boolean generate) {
		Thread thread = new Thread() {
			public void run() {
				mainFrame.disableSolveButtons();
				do {
				} while(solver.solveStep());
				
				if (generate) {
					board.invertMask();
					board.removeRandomFields();
				}
				
				mainFrame.repaint();
				mainFrame.enableSolbeButtons();
			}
		};
		thread.start();
	}
	
	private void paintThread(final Solver solver) {
		Thread thread = new Thread() {
			public void run() {
				solver.solve();
				mainFrame.repaint();
			}
		};
		thread.start();
	}
	
	
	public void solveBoard(boolean solverType, boolean tempChangeFunc, double initTemp, double tempChange,
			boolean printConsole, boolean printFile, int neighNumber, boolean neighFunc) {
		RUNNING = true;
		
		if (board == null)
			return;
		board.clear();
		mainFrame.repaint();
		mainFrame.setNewMap(board.getSudoku(), board.getMask());
		Solver solver;
		if (solverType) {
			solver = new AnealingSolver(board, "AnealingSolveLog.txt", tempChangeFunc, initTemp, tempChange, 
					printConsole, printFile, neighNumber, neighFunc);
			solver.init();	
			paintThread(solver, false);
		} else {
			solver = new BruteForceSolver(board);
			solver.init();	
			paintThread(solver);
		}

	}
	

	public static Main getMain() {
		return main;
	}

}
